package cn.hui.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FreeMarkerUtils {
	@SuppressWarnings("deprecation")
	public static boolean createFile(File file, String tempStr, Map<String, Object> root) {
		try {
			Configuration cfg = new Configuration();
			File directory = new File("");// 参数为空
			// 项目路径
			String courseFile = directory.getCanonicalPath();
			// 模版路径
			String templateDir = courseFile + "/mybatis-gen/src/main/resources/ftl/mybatis";
			cfg.setDefaultEncoding("UTF-8");
			File tp = new File(templateDir);
//			if (!tp.getParentFile().exists()) {
//				tp.getParentFile().mkdirs();
//			}
			cfg.setDirectoryForTemplateLoading(tp);
			Template temp = cfg.getTemplate(tempStr);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
			temp.process(root, osw);
			osw.flush();
			osw.close();
			return true;
		} catch (Exception exception) {
			log.error(exception.getMessage());
		}
		return false;
	}
}
